package com.SparkCore.RDD.persist

import org.apache.spark.rdd.RDD
import org.apache.spark.storage.StorageLevel
import org.apache.spark.{SparkConf, SparkContext}

/**
 * 持久化
 * checkPoint
 */
object Spark03_RDD_Persist {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("WordCount")
    val sc: SparkContext = new SparkContext(sparkConf)
    sc.setCheckpointDir("cp")

    //创建RDD算子
    val path = "datas/1.txt"
    val txt: RDD[String] = sc.textFile(path)

    val flatRDD: RDD[String] = txt.flatMap(_.split(" "))

    val mapRDD: RDD[(String, Int)] = flatRDD.map(word => {
      println("+++++++++++")
      (word, 1)
    })
    //checkpoint 需要落盘，需要指定检查点保存路径
    //检查点路径保存的文件，当作业执行完毕后，不会被删除
    //一般路径都是在分布式存储系统：HDFS
    mapRDD.checkpoint()

    val reduceRDD: RDD[(String, Int)] = mapRDD.reduceByKey(_+_)

    reduceRDD.collect().foreach(println)

    println("====================")

    val reduceRDD1: RDD[(String, Iterable[Int])] = reduceRDD.groupByKey()

    reduceRDD1.collect().foreach(println)
    //关闭链接
    sc.stop()
  }
}
